import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
// Set config defaults when creating the instance
const httpInstance = axios.create({
    baseURL: 'https://geek.itheima.net/v1_0',
    timeout: 5000
  });
  
// 添加请求拦截器
httpInstance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
httpInstance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
  }, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  });

  type resType<T> = {
    code: number
    message: string
    data:T
  }
//   axios+ts
const httpInstanceTs = <T>(options: AxiosRequestConfig) => {
    return new Promise<resType<T>>((resolve, reject) => {
        httpInstance.request(options).then((response:AxiosResponse) => {
            console.log('response',response);
            resolve(response.data)
        },(error) => {
            console.error('error', error);
            reject(error)
            throw new Error(error)
        })
    })
}
  
export {httpInstanceTs}